<template>
  <div class="pad-content-box">
    <form-common class="margin-top-40" v-if="organizationList.length > 0" :filed-list="organizationList" ref="form"></form-common>
    <el-button type="primary" size="mini" class="f-right" @click="saveFn">保存</el-button>
  </div>
</template>

<script>
import { organizationList } from './form';
import formCommon from '@g/components/form/formCommon.vue';
import { apiProjectCorrelation, apiProjectCorrelationAdd } from '../../api/projectInfo';
import Pooject from '../../api/projectInfo';

export default {
  name: 'projectMange',
  data() {
    return {
      organizationList: [],
      filedTypeList: [
        { type: 1, name: 'projectOperation' },
        { type: 2, name: 'projectProperty' },
        { type: 3, name: 'projectCustomer' },
        { type: 4, name: 'projectSales' },
      ],
    };
  },

  created() {
    this.initSelectData();
  },

  methods: {
    //初始化
    async initSelectData() {
      let [operation, property, customer, sale] = await Promise.all([this.getSelectOption(1, 'projectOperation'), this.getSelectOption(2, 'projectProperty'), this.getSelectOption(3, 'projectCustomer'), this.getSelectOption(4, 'projectSales')]);
      this.organizationList = organizationList;
      this.$nextTick(() => {
        this.$refs.form.initModel();
        this.getInfo();
      });
    },
    //详情
    async getInfo() {
      let res = await Pooject.apiProjectCorrelationInfo({ projectId: this.$route.query.projectKey });
      let model = {
        projectOperation: [],
        projectProperty: [],
        projectCustomer: [],
        projectSales: [],
      };
      if (res.organizationMap) {
        for (let i in res.organizationMap) {
          let item = res.organizationMap[i];
          this.filedTypeList.forEach(field => {
            if (i == field.type) {
              item.forEach(org => {
                model[field.name].push(org.organizationKey);
              });
            }
          });
        }
      }
      this.setFiledVal(this.organizationList, model);
    },
    //回显
    setFiledVal(list, row) {
      list.forEach(item => {
        for (let i in row) {
          if (i == item.bindFieldName) {
            item.bindFieldVal = row[i];
          }
        }
      });
    },
    //保存
    async saveFn() {
      let params = {
        list: [],
      };
      let model = this.$refs.form.validateForm(); //基础信息
      if (model === false) return;
      this.filedTypeList.forEach(field => {
        model[field.name].forEach(item => {
          params.list.push({ organizationType: field.type, organizationKey: item });
        });
      });
      params.projectId = this.$route.query.projectKey;
      let res = await Pooject.apiProjectCorrelationUpdate(params);
      if (res.success) {
        this.$message.success(res.message);
      } else {
        this.$message.error(res.message);
        this.$refs.form.initModel();
        this.getInfo();
      }
    },
    //下拉框数据源
    getSelectOption(paramType, filedName) {
      return new Promise(async (resolve, reject) => {
        let res = await Pooject.apiProjectCorrelation({
          orgProjectType: paramType,
        });
        organizationList.forEach(item => {
          if (item.bindFieldName == filedName) {
            item.optionList = res.rows || [];
          }
        });
        resolve(res.rows || []);
      });
    },
  },

  components: { formCommon },
};
</script>

<style>
</style>